package com.cst.words.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cst.words.common.R;
import com.cst.words.mapper.UserMapper;
import com.cst.words.pojo.User;
import com.cst.words.util.HttpRequest;
import com.cst.words.util.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author : CST
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;



    @PostMapping("/saveUserInfo")
    public R<String> saveUserInfo(HttpServletRequest request, @RequestBody User user) {

        // 获得openid
        String token = request.getHeader("token");
        Claims body = JwtUtils.parseJWT(token);
        String string = JSON.toJSONString(body);
        JSONObject jsonObject = JSONObject.parseObject(string);
        String openid = jsonObject.getString("openid");

        log.info(openid+"的用户更新后的信息是"+user);
        if (userMapper.updateById(user) == 1) {
            return R.success("更新用户信息存储成功");
        }
        return R.error("更新用户信息存储失败");


    }

}
